/*
 * DepartmentsDao.java
 *
 * Created on November 9, 2008, 7:35 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Departments;

/**
 *
 * @author JollkY
 */
public class DepartmentsDao implements Crud {
    
    private int state;
    private Departments department;
    private List departments;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of DepartmentsDao */
    public DepartmentsDao() {
    }

    
    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    
    public List<Object> getList() {
        departments = new ArrayList();
        try {
            em.getTransaction().begin();
            departments = em.createQuery("SELECT department FROM Departments department").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return departments;
    }

    
    public Object read(long id) {
        department = new Departments();
        try {
            em.getTransaction().begin();
            department = em.find(Departments.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return department;
    }

    
    public int update(Object obj) {
        department = new Departments();
        try {
            em.getTransaction().begin();
            Departments objDept = (Departments) obj;
            department = em.find(Departments.class, objDept.getId());
            department.setDepartmentChief(objDept.getDepartmentChief());
            department.setDepartmentName(objDept.getDepartmentName());
            department.setListWorker(objDept.getListWorker());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    
    public int delete(long id) {
        try {
            em.getTransaction().begin();
            department = em.find(Departments.class, id);
            em.remove(department);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }
    
}
